<template>
  <div class="register-container">
    <div class="floating-shapes">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>
    <div class="register-card">
      <div class="register-header">
        <div class="logo-container">
          <div class="logo-icon">
            <el-icon :size="40" color="#fff"><UserFilled /></el-icon>
          </div>
          <h2>加入课托邦</h2>
          <p class="welcome-text">欢迎来到ClassTopia智慧学习社区</p>
          <p class="subtitle">与同学分享知识，共同成长进步</p>
        </div>
      </div>
      
      <form @submit.prevent="register">
        <div class="input-group">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Message /></el-icon>
            <input v-model="email" type="email" placeholder="请输入邮箱" required />
          </div>
        </div>
        
        <div class="input-group">
          <div class="code-row">
            <div class="input-wrapper flex-1">
              <el-icon class="input-icon"><Key /></el-icon>
              <input v-model="code" type="text" placeholder="请输入验证码" required />
            </div>
            <button type="button" class="send-code" :disabled="loading" @click="sendCode">
              <el-icon class="btn-icon"><Message /></el-icon>
              发送验证码
            </button>
          </div>
        </div>
        
        <div class="input-group">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Lock /></el-icon>
            <input 
              v-model="password" 
              :type="showPassword ? 'text' : 'password'" 
              placeholder="请输入密码（至少6位）" 
              required 
            />
            <el-icon class="password-toggle" @click="showPassword = !showPassword">
              <View v-if="!showPassword" />
              <Hide v-else />
            </el-icon>
          </div>
        </div>
        
        <div class="input-group">
          <div class="input-wrapper">
            <el-icon class="input-icon"><Lock /></el-icon>
            <input 
              v-model="confirmPassword" 
              :type="showConfirmPassword ? 'text' : 'password'" 
              placeholder="请确认密码" 
              required 
            />
            <el-icon class="password-toggle" @click="showConfirmPassword = !showConfirmPassword">
              <View v-if="!showConfirmPassword" />
              <Hide v-else />
            </el-icon>
          </div>
        </div>
        
        <button class="register-btn" type="submit" :disabled="loading">
          <el-icon v-if="loading" class="loading-icon">
            <UserFilled />
          </el-icon>
          <span>{{ loading ? '注册中...' : '立即注册' }}</span>
        </button>
      </form>
      
      <div class="footer-links">
        <router-link to="/login" class="login-link">
          <el-icon class="link-icon"><User /></el-icon>
          已有账号？立即登录
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '../../stores/user'
import { useRouter } from 'vue-router'
import { registerUserApi, sendCodeApi } from '../../api/user'
import { ElMessage } from 'element-plus'
import { UserFilled, User, Lock, Message, Key, View, Hide } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const email = ref('')
const code = ref('')
const password = ref('')
const confirmPassword = ref('')
const loading = ref(false)
const showPassword = ref(false)
const showConfirmPassword = ref(false)

function validateEmail(email: string) {
  // 简单邮箱正则
  return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
}

function sendCode() {
  if (!email.value) {
    ElMessage.error('请先填写邮箱！')
    return
  }
  if (!validateEmail(email.value)) {
    ElMessage.error('邮箱格式不正确！')
    return
  }
  loading.value = true
  sendCodeApi(email.value)
    .then(() => {
      ElMessage.success('验证码已发送到邮箱！')
    })
    .catch((error) => {
      ElMessage.error(error.response?.data?.message || '验证码发送失败，请稍后再试！')
    })
    .finally(() => {
      loading.value = false
    })
}

async function register() {
  if (!email.value) {
    ElMessage.error('请填写邮箱！')
    return
  }
  if (!validateEmail(email.value)) {
    ElMessage.error('邮箱格式不正确！')
    return
  }
  if (!code.value) {
    ElMessage.error('请填写验证码！')
    return
  }
  if (password.value.length < 6) {
    ElMessage.error('密码至少6位数！')
    return
  }
  if (password.value !== confirmPassword.value) {
    ElMessage.error('两次输入的密码不一致！')
    return
  }
  
  loading.value = true
  try {
    const res = await registerUserApi({
      email: email.value,
      password: password.value,
      code: code.value
    })
    
    ElMessage.success('注册成功!')
    
    // 存储用户信息到store
    userStore.id = res.data.id || ''
    userStore.nickname = ''
    userStore.email = email.value
    userStore.birthday = ''
    userStore.gender = ''
    userStore.bio = ''
    userStore.avatar = ''
    userStore.token = res.data.token || ''
    
    router.push('/welcome') // 注册成功后跳转,填写详细信息
  } catch (error: any) {
    ElMessage.error(error.response?.data?.message || '注册失败，请稍后再试！')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  min-width: 100vw;
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #a9c5f7 100%);
  z-index: 1000;
  overflow: hidden;
}

/* 浮动背景装饰 */
.floating-shapes {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.shape {
  position: absolute;
  border-radius: 50%;
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  background: #fff;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  background: #e9ddfe;
  top: 20%;
  right: 10%;
  animation-delay: 2s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  background: #a259ec;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.register-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(90, 80, 200, 0.2);
  padding: 38px 30px 30px 30px;
  min-width: 400px;
  max-width: 450px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  position: relative;
  z-index: 10;
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.register-header {
  margin-bottom: 32px;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.logo-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
  }
  50% {
    box-shadow: 0 4px 30px rgba(102, 126, 234, 0.5);
  }
  100% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
  }
}

.register-card h2 {
  color: #333;
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.welcome-text {
  color: #667eea;
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
}

.subtitle {
  color: #999;
  margin: 0;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.4;
}

form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 16px;
  color: #999;
  font-size: 18px;
  z-index: 2;
}

.password-toggle {
  position: absolute;
  right: 16px;
  color: #999;
  font-size: 18px;
  cursor: pointer;
  z-index: 2;
  transition: color 0.2s;
}

.password-toggle:hover {
  color: #667eea;
}

input[type="email"],
input[type="password"],
input[type="text"] {
  padding: 16px 50px 16px 50px;
  border: 2px solid #eef2ff;
  border-radius: 12px;
  font-size: 16px;
  outline: none;
  transition: all 0.3s ease;
  background: #fff;
  width: 100%;
  box-sizing: border-box;
}

input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  transform: translateY(-1px);
}

.flex-1 {
  flex: 1;
}

.code-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.send-code {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 16px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
}

.send-code:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
}

.send-code:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-icon {
  font-size: 16px;
}

.register-btn {
  margin-top: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 16px 0;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.2);
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(102, 126, 234, 0.3);
}

.register-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.loading-icon {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.footer-links {
  margin-top: 24px;
  text-align: center;
}

.login-link {
  color: #667eea;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}

.login-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

.link-icon {
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-card {
    margin: 20px;
    padding: 32px 24px;
    min-width: auto;
    max-width: none;
  }
  
  .code-row {
    flex-direction: column;
    gap: 12px;
  }
  
  .send-code {
    width: 100%;
    justify-content: center;
  }
}
</style>
